<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 16/8/18
 * Time: 上午11:16
 */
use app\assets\AppAsset;
AppAsset::register($this);

$base = Yii::$app->request->baseUrl;
AppAsset::addCss($this, $base . "/css/shop-bag.css");
$this->title = '我的收藏';
$share_id= \Yii::$app->user->id;
if(!$share_id){
    $share_id=-1;
}
?>
<div style="padding-left: 15px;padding-right: 15px" id="shop_bag" data-role="page">

    <section class="gwc_section">
        <div class="product_title">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="24px"><img class="shop_icon" src="<?= $base ?>/images/home_icon.png"/>
                    </td>
                    <td><span><?=$shopName?></span></td>
                    <td align="right"><span id="edit_parent" onclick="myDelete()">编辑</span>
                    </td>
                </tr>
            </table>
        </div>
        <?php for ($j = 0; $j < count($commodities); $j++) { ?>
            <div class="product_hover" >
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>

                        <td width="90px"><div class="image-div"><img class="product_icon"
                                                                     src="<?= $commodities[$j]['pic_url'] ?>"/></div></td>
                        <td style="padding-left: 10px;line-height: 100%" onclick="clickToCommodity(<?= $commodities[$j]['commodity_id'] ?>)">
                            <p style="height: 30px;overflow: hidden;"><?= $commodities[$j]['commodity_name'] ?></p>

                            <p style="color: #FFA500;">￥<?= $commodities[$j]['price'] ?></p>
                        </td>
                        <td id="goods_<?= $j ?>" style="display: none;background:#FF0033;width:50px "
                            align="right" onclick="deleteCollection(<?= $collections[$j]['collection_id']?>)">
                            <p style="text-align:center;color: #ffffff">删除</p>
                        </td>
                    </tr>
                </table>
            </div>
        <?php } ?>
<!--        <div style="border: 1px solid #e5e5e5;margin-top: 5px;"></div>-->
        <?php for ($j = 0; $j < count($commodities_out_date); $j++) { ?>
            <div class="product_hover" >
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>

                        <td width="90px"><div class="image-div"><img class="product_icon"
                                                                     src="<?= $commodities_out_date[$j]['pic_url'] ?>"/></div></td>
                        <td style="padding-left: 10px;line-height: 100%" onclick="clickToCommodity(<?= $commodities_out_date[$j]['commodity_id'] ?>)">
                            <p style="height: 15px;overflow: hidden;"><?= $commodities_out_date[$j]['commodity_name'] ?></p>
                            <p style="line-height: 100%;font-size: 12px;color: #aaa">您收藏的商品已过期</p>
                            <p style="color: #FFA500;">￥<?= $commodities_out_date[$j]['price'] ?></p>
                        </td>
                        <td id="goods_<?= count($commodities)+$j ?>" style="display: none;background:#FF0033;width:50px "
                            align="right" onclick="deleteCollection(<?= $collections[$j]['collection_id']?>)">
                            <p style="text-align:center;color: #ffffff">删除</p>
                        </td>
                    </tr>
                </table>
            </div>
        <?php } ?>
    </section>

    <div style="height: 45px"></div>


</div>
<script>
    $(document).ready(function () {
        function imageLoaded() {
            $(this).attr("hidden", false);
            var height = $(this).css('height');

            var width = $(this).css('width');
            if (parseInt(height.substring(0, height.length - 2)) > parseInt(width.substring(0, width.length - 2))) {
                $(this).css('height', "100%");
                $(this).width('auto');
            } else {
                $(this).css('width', "100%");
                $(this).height('auto');
            }
        }

        $(".product_icon").each(function () {
            if (this.complete) {
                imageLoaded.call(this);
            } else {
                $(this).one('load', imageLoaded);
            }
        });
    });
//    var collection = (<?php //echo json_encode($collection)?>//);
    var collections = (<?php echo json_encode($collections)?>);


    function myDelete() {
        for (var i = 0; i < collections.length; i++) {
            if (document.getElementById("goods_" + i).style.display == "none") {
                document.getElementById("edit_parent").text = "完成";
                document.getElementById("goods_" + i).style.display = "";
            } else {
                document.getElementById("edit_parent").text = "编辑";
                document.getElementById("goods_" + i).style.display = "none";
            }
        }
    }
    function deleteCollection(collection_id){
        $.ajax({
//            type:"POST",
//            dataType:"json",
            data:{"collection_id":collection_id},
            url:"delete-collection",
            success:function(json){
//                console.log("123",json);
                alert("删除成功");
                location.reload();
            },
            error:function(data){
//                console.log(data);
                alert("删除失败");
            }
        });
    }
    function clickToCommodity(commodity_id) {
        location.href = '../commodity-detail/detail?commodity_id=' + commodity_id+"&share_id="+<?php echo $share_id?>;
    }
</script>
